package com.model.demo.spring.framework.beans.support;

import com.model.demo.spring.framework.beans.config.MyBeanDefinition;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
///用对配置文件进行查找，读取、解析
public class MyBeanDefinitionReader {

    private Properties config = new Properties();

    private List<String> classBeanNames = new ArrayList<>();

    private final String SCAN_PACKAGE = "scanPackage";


    public MyBeanDefinitionReader(String... locations) {
        try {
            config.load(this.getClass().getClassLoader().getResourceAsStream(locations[0].replace("classpath:", "")));
            doScanPackage(config.getProperty(SCAN_PACKAGE));
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private void doScanPackage(String scanBasePackage) {
        //Question
        //getClassLoader获取不到路径,路径转换
        URL resource = this.getClass().getResource("/" + scanBasePackage.replaceAll("\\.","/"));
        File rootFile = new File(resource.getFile());
        for(File file:rootFile.listFiles()){

            if(file.isDirectory()){
                doScanPackage(scanBasePackage + "." + file.getName());
            }else{
                if(!file.getName().endsWith(".class")){continue;}
                String className = (scanBasePackage + "." +file.getName()).replace(".class","");
                classBeanNames.add(className);
            }
        }


    }

    public List<MyBeanDefinition> loadBeanDefinitions() {
        List<MyBeanDefinition> beanDefinitions = new ArrayList<>();
        for (String classBeanName : classBeanNames) {
            List<MyBeanDefinition> beanDefinition = doCreateBeanDefinition(classBeanName);
            if(beanDefinition != null && !beanDefinition.isEmpty()){
                beanDefinitions.addAll(beanDefinition);
            }
        }

        return beanDefinitions;
    }

    private List<MyBeanDefinition> doCreateBeanDefinition(String classBeanName) {
        List<MyBeanDefinition> result = new ArrayList<>();
        try {
            Class<?> bean = Class.forName(classBeanName);
            if(bean.isInterface()){
                return null;
            }
            //beanName有三种情况
            //1.默认类名首字母小写
            //2.自定义名字
            //3.接口注入
            result.add(new MyBeanDefinition(bean.getName(), toLowerFirstCase(bean.getSimpleName())));
            result.add(new MyBeanDefinition(bean.getName(), bean.getName()));
            Class<?>[] interfaces = bean.getInterfaces();
            if(interfaces !=null){
                for (Class<?> i : interfaces) {
                    //可以是多个接口的实现类，不同接口(键)同一类名(值)
                    result.add(new MyBeanDefinition(bean.getName(), i.getName()));
                }
            }
            return result;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    private String toLowerFirstCase(String simpleName) {
        char [] chars = simpleName.toCharArray();
        chars[0] += 32;
        return  String.valueOf(chars);
    }

    public Properties getConfig() {
        return config;
    }
}
